package designPattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//容器式单例：对枚举式的优化，提高效率
public class ContainerSingleton {
    private ContainerSingleton() {

    }
    private static Map<String, Object> iocMap = new ConcurrentHashMap<String, Object>();

    public static Object getInstance(String className) {
        Object instance = null;
        if(iocMap.containsKey(className)) {
            instance = iocMap.get(className);
        } else {
            try {
                instance = Class.forName(className).newInstance();
                iocMap.put(className, instance);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return instance;
    }

}
